<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-cf">分享树状图</div>
                </div>
                <div class="widget-body am-fr">
                     <div class="page_toolbar am-margin-bottom-xs am-cf">
                        <form class="toolbar-form" action="">
                            <input type="hidden" name="s" value="/<?= $request->pathinfo() ?>">
                            <div class="am-u-sm-12 am-u-md-3">
                                <div class="am-form-group">
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            
                            
                                        </div>
                                    </div>
                                </div>
                            </div>
                           
                            <div class="am-u-sm-12 am-u-md-9">
                                <div class="am fr">
                                    
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <input type="text" name="start_time"
                                               class="am-form-field"
                                               value="<?= $request->get('start_time') ?>" placeholder="请选择起始日期"
                                               data-am-datepicker>
                                    </div>
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <input type="text" name="end_time"
                                               class="am-form-field"
                                               value="<?= $request->get('end_time') ?>" placeholder="请选择截止日期"
                                               data-am-datepicker>
                                    </div>
                                    <div class="am-form-group am-fl">
                                        <div class="am-input-group am-input-group-sm tpl-form-border-form">
                                            <input type="text" class="am-form-field" name="keyword"
                                                   placeholder="用户ID/用户昵称/手机号/姓名" value="<?= $request->get('keyword') ?>">
                                            <div class="am-input-group-btn">
                                                <button class="am-btn am-btn-default am-icon-search"
                                                        type="submit"></button>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- 工具栏里搜索框旁边加导出按钮 -->
                                    <div class="am-form-group am-fl">
                                    <div class="am-btn-group am-btn-group-sm">
                                        <button id="btn-export" class="am-btn am-btn-secondary" type="button">
                                        <i class="am-icon-download"></i> 导出
                                        </button>
                                    </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="am-u-sm-12">
                        <style>
                            .sub-cat {
                                display: none;
                            }

                            /* 级别颜色 */
                            .level-1 {
                                background-color: #f2f9ff;
                            }

                            .level-2 {
                                background-color: #fef6e7;
                            }

                            .level-3 {
                                background-color: #eaf7ea;
                            }

                            .level-4 {
                                background-color: #fce8ef;
                            }

                            .level-5 {
                                background-color: #f5f5f5;
                            }

                            /* 图标对齐 */
                            .toggle-sub {
                                cursor: pointer;
                                margin-right: 5px;
                            }
                        </style>

                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black">
                            <thead>
                            <tr>
                                <th>用户 ID</th>
                                <th>用户名</th>
                                <th>电话</th>
                                <th>级别</th>
                                <th>时间</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if (!empty($tree)): ?>
                                <?php
                                function renderTree($node, $level = 1, $parentId = 0) {
                                    $hasChild = !empty($node['child']);
                                    $rowClass = $level == 1 ? 'parent-cat level-'.$level : 'sub-cat level-'.$level;
                                    $rowAttr = $level == 1
                                        ? 'data-id="'.$node['category_id'].'"' 
                                        : 'data-parent="'.$parentId.'" data-id="'.$node['category_id'].'"';

                                    $toggleIcon = $hasChild
                                        ? '<i class="am-icon-plus-square toggle-sub" data-id="'.$node['category_id'].'"></i>'
                                        : '<span style="display:inline-block;width:14px;"></span>';

                                    $name = empty($node['name']) ? '未知姓名' : $node['name'];
                                    $phone = isset($node['phone']) && $node['phone'] !== '' ? $node['phone'] : '—';

                                    echo '<tr class="'.$rowClass.'" '.$rowAttr.'>';
                                    echo '<td class="am-text-middle">'.$node['category_id'].'</td>';
                                    echo '<td class="am-text-middle">'.$toggleIcon.$name.'</td>';
                                    echo '<td class="am-text-middle">'.$phone.'</td>';
                                    echo '<td class="am-text-middle">'.$level.'</td>';
                                    echo '<td class="am-text-middle">'.$node['create_time'].'</td>';
                                    echo '</tr>';

                                    if ($hasChild) {
                                        foreach ($node['child'] as $child) {
                                            renderTree($child, $level + 1, $node['category_id']);
                                        }
                                    }
                                }
                                foreach ($tree as $node) {
                                    renderTree($node);
                                }
                                ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="4" class="am-text-center">暂无记录</td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        // 默认折叠所有子项
        $('tr.sub-cat').hide();

        // 折叠/展开行为
        $(document).on('click', '.toggle-sub', function () {
            const $icon = $(this);
            const catId = $icon.data('id');
            const isOpen = $icon.hasClass('am-icon-minus-square');

            if (isOpen) {
                $icon.removeClass('am-icon-minus-square').addClass('am-icon-plus-square');
                hideAllChildren(catId);
            } else {
                $icon.removeClass('am-icon-plus-square').addClass('am-icon-minus-square');
                showDirectChildren(catId);
            }
        });

        function hideAllChildren(parentId) {
            $('tr[data-parent="' + parentId + '"]').each(function () {
                const $row = $(this);
                const childId = $row.data('id');
                $row.hide();
                // 收起图标
                $row.find('.toggle-sub').removeClass('am-icon-minus-square').addClass('am-icon-plus-square');
                // 递归隐藏子项
                hideAllChildren(childId);
            });
        }

        function showDirectChildren(parentId) {
            $('tr[data-parent="' + parentId + '"]').show();
        }
        // 导出当前筛选结果
        $('#btn-export').on('click', function () {
            // 只取除 name="s" 以外的参数
            var params = $('.toolbar-form')
                .find(':input')
                .not('[name="s"]') // 排除 s
                .serialize();

            // 用 ThinkPHP 的 url() 生成基础路径
            var baseUrl = "<?= url('store/apps.dealer.withdraw/export') ?>";
            window.location = baseUrl + (params ? ('?' + params) : '');
        });
    });
</script>